package com.kinghunt.misp.web.controller.roomx;

import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.dto.page.PageInfo;
import com.kinghunt.misp.common.dto.roomx.RoomDTO;
import com.kinghunt.misp.common.dto.userx.UserDTO;
import com.kinghunt.misp.common.param.roomx.RoomParam;
import com.kinghunt.misp.service.api.roomx.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

import static com.kinghunt.misp.web.context.SystemContext.getCurrentUser;

/**
 * 房间管理
 *
 * @Author ciaran
 * @Date 2023/7/13 21:58l
 */
@RestController
@RequestMapping("/api/room/")
public class RoomController {

    @Autowired(required = false)
    private RoomService roomService;

    /**
     * 列表显示：显示房间列表
     */
    @GetMapping("getRoomList")
    public ApiResult<PageInfo<RoomDTO>> getRoomList(RoomParam roomParam) {
        return roomService.getRoomList(roomParam);
    }
    /**
     * 房间详情展示
     */
    @GetMapping("getRoomDetail")
    public ApiResult<RoomDTO> getRoomDetail(Long RoomId) {
        if(Objects.isNull(RoomId)){
            return ApiResult.failOfMessage("查询失败，请输入房间信息！");
        }
        return roomService.getRoomDetail(RoomId);
    }
    /**
     * 新增房间
     */
    @PostMapping(value = "addRoom", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult addRoom(@Valid @RequestBody RoomParam roomParam) {
        UserDTO loginUser = getCurrentUser();
        roomParam.setCreatorId(loginUser.getId());
        roomParam.setUpdaterId(loginUser.getId());
        return roomService.addRoom(roomParam);
    }
    /**
     * 修改房间
     */
    @PostMapping(value = "updateRoom", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult updateRoom(@Valid @RequestBody RoomParam roomParam) {
        UserDTO loginUser = getCurrentUser();
        roomParam.setUpdaterId(loginUser.getId());
        return roomService.updateRoom(roomParam);
    }
    /**
     * 删除房间
     *
     * @param RoomId 角色ID
     * @return 是否成功
     */
    @DeleteMapping("deleteRoom")
    public ApiResult<Boolean> deleteRoom(Long RoomId) {
        if(Objects.isNull(RoomId)){
            return ApiResult.failOfMessage("删除失败，请输入房间信息！");
        }
        return roomService.deleteRoom(RoomId);
    }

}
